<template>
	<view style="min-height: 100vh;">
		<u-navbar title='材料' titleStyle="{'color':white}" :autoBack="true" bgColor="transparent"
			:placeholder="false"></u-navbar>
		<image
			src="https://renovationcdn.jiachuangnet.com/addMaterial/homeBg.png"
			style="width:750rpx;height: 100vh;position: fixed;"></image>
		<image src="https://renovationcdn.jiachuangnet.com/addMaterial/text.png" style="width: 498rpx;height: 55rpx;top: 200rpx;left:25rpx;position: fixed;z-index: 1;"></image>
		<view class="container">
			<view class="shopSelect flex bewteen alignItem">
				<view style="flex:4;text-align: center;">
					名称
				</view >
				<view style="flex:1;text-align: center;">
					单位
				</view>
				<view style="flex:2;text-align: center;">
					价格
				</view>
				<view style="flex:3;text-align: center;">
					操作
				</view>
			</view>
			<view style="border-radius: 10rpx;overflow: hidden;">
				<view v-for="(v,i) in list" :key="i" :class="i == 0?'topItem':'item'">
					<view >
						<view class="unit flex bewteen alignItem">
							<view class='name' style="word-break:break-all;">
								{{v.name}}
							</view>
							<view class="unitValue">
								{{v.unit}}
							</view>
							<view class="sale">
								{{v.salePrice}}
							</view>
							<view class="flex bewteen alignItem" style="padding:20rpx;flex: 3;justify-content: center;">
								<view>
									<uni-number-box v-model="v.buyCount" :max='100000' :min="0"></uni-number-box>
								</view>
							</view>
						</view>
						
					</view>
				
				</view>
			</view>
			

		</view>
		<view class="bottom flex alignItem" :style="{paddingBottom: bottom+'px'}" style="justify-content: center;">
			<view class="bottomleft flex" style="justify-content: center;">
				<view>
					 <text style="margin: 0 10rpx;font-size: 32rpx;">{{totalPayCost}}</text>
				</view>
				<view>
					已选<text style="color:#ED115E">{{totalCount}}</text>件商品
				</view>
			</view>
			<view class="bottomright" @click="addWechatOrder">
				提交材料</view>
		</view>
	</view>
</template>
<script>
	
	import Decimal from 'decimal.js'
	let app = getApp();
	export default {
		data() {
			return {
				bottom:app.globalData.safeBottom,
				list: [],
				totalCount: new Decimal(0),
				totalPayCost: new Decimal(0),
				assignTaskId:0,
				// 提交的节流控制阀门
				timer:'',
				flag:0,
				storeType:null,
				state:2,
				afterId:null
			}
		},
		watch: {
			'list': {
				handler(newVal) {
					this.totalCount = 0;
					this.totalPayCost = 0;
					newVal.map(el => {
						if(el.buyCount&&el.buyCount!=0){
							this.totalCount = Decimal(this.totalCount).plus(Decimal(el.buyCount));
							this.totalPayCost = Decimal(this.totalPayCost).plus(Decimal(el.salePrice).mul(Decimal(el
								.buyCount)))
						}
					})
				},
				deep: true,
				immediate: true
			}
		},
		onLoad(option) {
			this.assignTaskId = option.assignTaskId
			this.storeType=option.storeType
			if(option.state){
				this.state=3
				this.afterId= option.afterId
			}
			this.getClassificationList()
		},
		methods: {
			getClassificationList(){
				uni.$u.http.get('/materials/getClassificationList',{
					params:{
						storeType:this.storeType
					}
				}).then(res=>{
					console.log(res,'获取所有材料')
					if(res){
						this.list=res
					}
				})
			},
			handlerList(){
				return this.list.filter(item=>{
					if(item.buyCount&&item.buyCount!=0){
						return {
							classificationId:item.classificationId,
							buyCount:item.buyCount,
							name:item.name,
							salePrice:item.salePrice
						}
					}
				})
			},
			addWechatOrder(){
				if(this.flag==1){
					return uni.showLoading({
						title: '请勿重复提交 :)'
					});
				}
				if(this.timer){
					return uni.$u.toast('提交太频繁了!');
				}
				this.timer=setTimeout(()=>{
					this.timer=null
				},3000)
				
				let listResult=this.handlerList()
				if(listResult.length===0){
					return uni.$u.toast('下单材料不能为空');
				}
				this.flag=1
				uni.$u.http.post('/materials/addWechatOrder',{
						assignTaskId: this.assignTaskId,
						list:listResult,
						afterId:this.afterId||null,
						totalPrice:Number(this.totalPayCost),
						 // 1 报货 2 补货 3售后补货
						state:this.state
					}
				).then(res=>{
					console.log(res,'获取所有材料')
					if(res){
						uni.showToast({
							title: '下单成功!',
							duration: 2000
						})
						this.flag=0
						uni.navigateBack({
							delta: 1
						});
					}
				})
			}
			
		}
	}
</script>
<style lang="scss" scoped>
	.container {
		width: 700rpx;
		padding-bottom: 200rpx;
		z-index: 10;
		position: absolute;
		left: 25rpx;
		top: 320rpx;
		overflow: hidden;
		border-radius: 10rpx;
	}

	.item {
		width: 700rpx;
		background-color: #ffffff;
	}

	.topItem {
		width: 700rpx;
		background-color: #ffffff;
	}

	.shopSelect {
		padding: 30rpx 20rpx 40rpx 20rpx;
		font-size: 32rpx;
		color: white;
		background-color:rgba(255,255,255,0.25);
		margin-bottom: -10rpx;
	}

	.name {
		// padding: 30rpx 20rpx;
		font-size: 30rpx;
		color: #1E272E;
		text-align: center;
		flex: 4;
	}

	.unit {
		height: 115rpx;
		margin: 0 20rpx;
		border-bottom: 1px solid #eeeeee;
	}

	.unitKey {
		color: #8A8A8A;
		font-size: 28rpx;
		text-align: center;
	}
	.sale{
		color: #1E272E;
		text-align: center;
		font-size: 28rpx;
		margin-top: 10rpx;
		text-align: center;
		flex: 2;
	}

	.unitValue {
		color: #1E272E;
		text-align: center;
		font-size: 28rpx;
		margin-top: 10rpx;
		text-align: center;
		flex: 1;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: white;
		z-index: 10000;
		width: 100%;
		padding:20rpx 0;
		min-height: 20rpx;
	}

	.bottomleft {
		height: 88rpx;
		width: 450rpx;
		border-bottom-left-radius: 80rpx;
		border-top-left-radius: 80rpx;
		background-color: #222426;
		color: white;
		font-size: 24rpx;
		text-align: center;
		line-height:88rpx;
	}

	.bottomright {
		width: 250rpx;
		height: 88rpx;
		border-bottom-right-radius: 80rpx;
		border-top-right-radius: 80rpx;
		background-color: #13989A;
		color: white;
		font-size: 30rpx;
		text-align: center;
		line-height:88rpx;
		;
	}
	.container /deep/ .uni-numbox__minus{
		border: #13989A 1rpx solid;
		border-radius: 50%;
		background-color: transparent !important;
		width:40rpx;
		padding:0 !important;
		height: 40rpx;
		text{
			color: #13989A !important;
		}
	}
	.container /deep/ .uni-numbox__value{
		background-color: transparent !important;
	}
	.container /deep/ .uni-numbox__plus{
		background-color: #13989A !important;
		width:40rpx;
		padding:0 !important;
		height: 40rpx;
		border-radius: 50%;
		color:white !important;
		text{
			color: white !important;
		}
	}
</style>
<style>
	page {
		background-color: #f8f8f8;
	}
</style>